//
//  HSYCollectionViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/1.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYBaseCollectionViewController.h"
#import <Masonry.h>
#import "MJRefresh.h"

@interface HSYBaseCollectionViewController ()

@end

@implementation HSYBaseCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.pageIdex = 0;
    self.pageStep = 10;

    [self.view addSubview:self.collectionView];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo([self hsy_collectionViewEdgeInsets]);
    }];
    
    [self addRefreshFooterIfNeed];
    [self addRefreshHeaderIfNeed];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 加载tableview -

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowlayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

#pragma mark - Private Methods -
- (void)addRefreshFooterIfNeed{
    if ([self hsy_allowPullUp]) {
        __weak typeof(self) wSelf = self;
        self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            __strong typeof(wSelf) strongSelf = wSelf;
            [strongSelf hsy_handlePullUpToLoadMore];
        }];
    }
}

- (void)addRefreshHeaderIfNeed{
    __weak typeof(self) wSelf = self;
    if ([self hsy_allowPullDown]) {
        self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong typeof(wSelf) strongSelf = wSelf;
            [strongSelf hsy_handlePullDownToLoadNew];
        }];
    }
}

#pragma mark - Public Methods -

- (UIEdgeInsets)hsy_collectionViewEdgeInsets{
    return UIEdgeInsetsZero;
}

- (BOOL)hsy_allowPullUp{
    return YES;
}

- (BOOL)hsy_allowPullDown{
    return YES;
}

- (void)hsy_headerEndRefreshing{
    [self.collectionView.mj_header endRefreshing];
}

- (void)hsy_footerEndRefreshing{
    [self.collectionView.mj_footer endRefreshing];
}

- (void)hsy_footerBeginRefreshing{
    [self.collectionView.mj_footer beginRefreshing];
}

- (void)hsy_headerBeginRefreshing{
    [self.collectionView.mj_header beginRefreshing];
}

- (void)hsy_footerEndRefreshingWithNoMoreData{
    [self.collectionView.mj_footer endRefreshingWithNoMoreData];
}

- (void)hsy_handlePullDownToLoadNew{
    self.pageIdex = 0;
    self.collectionView.mj_footer.automaticallyHidden = YES;
    [self hsy_loadServerData];
}

- (void)hsy_handlePullUpToLoadMore{
    self.pageIdex ++;
    [self hsy_loadServerData];
}

- (void)hsy_loadServerData{
    
}


#pragma mark - UITableViewDelegate & UITableViewDataSource -

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 0;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    return nil;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
